package com.itheima.tlias.controller;

import com.itheima.tlias.comment.MyComment;
import com.itheima.tlias.dto.AddEmpDto;
import com.itheima.tlias.dto.ChangePasswordDto;
import com.itheima.tlias.dto.EmpListDto;
import com.itheima.tlias.dto.UpdateEmpDto;
import com.itheima.tlias.entity.Emp;
import com.itheima.tlias.service.EmpService;
import com.itheima.tlias.utils.Constant;
import com.itheima.tlias.vo.EmpListVo;
import com.itheima.tlias.vo.EmpVo;
import com.itheima.tlias.vo.PageBean;
import com.itheima.tlias.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

   // @CrossOrigin(origins = {"http://127.0.0.1:5173","http://localhost/:5173","http://127.0.0.1:5174","http://localhost/:5174"})
    @GetMapping
    //大字段 @MyComment(desc = "员工条件 分页查询")
    public Result<PageBean<EmpListVo>> empPage(EmpListDto empListDto, HttpServletResponse response) {
     /*   response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500");
        response.addHeader("Access-Control-Allow-Methods", "*");
        response.addHeader("Access-Control-Allow-Headers", "*");*/
        PageBean<EmpListVo> pageBean = empService.empPage(empListDto);
        return Result.success(pageBean);

    }
    //大字段 @MyComment(desc = "员工新增，同时像emp_log表输出成功/失败日志")
    @PostMapping
    public Result<Void> addEmp(@RequestBody AddEmpDto addEmpDto) {
        empService.addEmp(addEmpDto);
        return Result.success();
    }
    @MyComment(desc = "员工批量删除")
    @DeleteMapping
    public Result<Void> deleteEmps(@RequestParam("ids") List<Integer> ids) {
        empService.deleteByIds(ids);
        return Result.success();
    }

    @MyComment(desc = "员工修改  回显功能")
    @GetMapping("{id}")
    public Result<EmpVo> getById(@PathVariable(Constant.ID) Integer id) {
        EmpVo empVo = empService.getById(id);
        return Result.success(empVo);

    }
    @MyComment(desc = "员工修改")
    @PutMapping
    public Result<Void> updateEmp(@RequestBody UpdateEmpDto dto) {
        empService.updateEmp(dto);
        return Result.success();

    }

    @MyComment(desc = "班级管理接口  查询班主任")
    @GetMapping("/list")
    public Result<List<Emp>> clazzs() {
        List<Emp> clazzs = empService.empListByJob();
        return Result.success(clazzs);
    }

    @MyComment(desc = "修改密码")
    @PutMapping("/changePassword")
    public Result<Void> amendPassword(@RequestBody ChangePasswordDto dto, HttpServletRequest servletRequest) {
        empService.amendPassword(dto, servletRequest);
        return Result.success();
    }
}
